//groupguard.txt - A guard who is part of a group. Goes on patrol occasionally. 
//When it encounters a hostile person, it fights for 2 rounds and then runs back to start point. 
//When it gets there, whole group is alerted and goes to attack.
//Cell 0 - Number of path to patrol. If 12 or over, stands still.
//  (Note: Walks to end of path, then paths back. Don't loop path back to start.)
//Cell 1 - Number of state when talked to. Plays default text if left at 0.
//Cell 2 - Number of creatures group. Creature will add itself to group. Ignored if 0.
// Cell 3,4 - which abil

begincreaturescript;

variables;

short alerted_time = 0;
short has_fled = 0;
short has_alerted_group = 0;
short has_been_alerted = 0;
short saw_message = 0;
short last_abil;
short used_abil = 0;

body;

beginstate INIT_STATE;
	if (get_memory_cell(2) > 0) {
		add_char_to_group(ME,get_memory_cell(2));
		}
	if (get_memory_cell(0) < 12)
		set_aggression(ME,8);

	if (get_level(ME) < 20)
		set_level(ME,20);
	if (get_level(ME) > 20)
		set_boss_level(ME,2);
		else set_boss_level(ME,1);

	last_abil = get_current_tick();
	break;

beginstate DEAD_STATE;
	if ((what_group_in(ME) > 0) && (has_alerted_group == 0) && (get_memory_cell(0) >= 12)) {
		alert_char(1000 + what_group_in(ME));
		give_char_message(1000 + what_group_in(ME),50);
		set_act_at_dist(1000 + what_group_in(ME),1);
		}
	break;

beginstate START_STATE; // going on patrol
	if ( my_current_message() == 50)
		has_been_alerted = 1;



	// if I have a target for some reason, go attack it
	if (target_ok()) {
		
		if (dist_to_char(get_target()) <= 32) {
			alerted_time = get_current_tick();
			set_state(3);
			}
			//else set_foe_target(ME,-1);
		}
		
	if (get_foe_target(ME,8,0)) {
		if (gf(54,9) == 0) {
			sf(54,9,1);
			begin_talk_mode(6);
			}
		alerted_time = get_current_tick();
		do_attack();
		set_state(3);
		}
	if (who_shot_me() >= 0) {
		alerted_time = get_current_tick();
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}

	if (my_dist_from_start() >= 2) {
		if (get_ran(1,1,100) < 85) 
			return_to_start(ME,1);
		}
		else if ((get_ran(1,1,100) < 6) && (get_memory_cell(0) < 12)) {
			follow_path(ME,get_memory_cell(0),0);
			set_state(4);
			}

	if (am_i_doing_action() == FALSE)
		end_combat_turn();

break;

beginstate 3; // attacking
	if ( my_current_message() == 50)
		has_been_alerted = 1;
	
	if ((saw_message == 0) && (has_been_alerted == 0)) {
		print_named_str(ME,"has seen you.");
		saw_message = 1;
		}	
	if ((tick_difference(alerted_time,get_current_tick()) >= 5) &&
	  (has_been_alerted == 0) && (has_alerted_group == 0)) {
		set_state(5);
		}

	if (target_ok() == FALSE)
		set_state(START_STATE);


	if (gf(54,9) == 0) {
		sf(54,9,1);
		begin_talk_mode(6);
		}
	
	used_abil = 0;
	
	if ((get_memory_cell(3) > 0) && (tick_difference(last_abil,get_current_tick()) > 0) && (is_combat())) {
		if ((get_char_status(ME,1) <= 0) && (get_ran(1,0,100) < 40)) {
			run_char_animation(2,1,35);	
			print_named_str(ME,"starts to move faster!");
			pc_heard_sound_delay(101,250);						
		  	place_particle_num(ME,3,0,8);
			set_char_status(ME,1,5);
			last_abil = get_current_tick();
			used_abil = 1;
			}
			else if ((get_char_status(ME,8) <= 0) && (get_ran(1,0,100) < 20)) {
				run_char_animation(2,1,35);	
				print_named_str(ME,"creates a magical shield.");
				pc_heard_sound_delay(101,250);						
		 	 	place_particle_num(ME,1,0,8);
				set_char_status(ME,8,6);
				last_abil = get_current_tick();
				used_abil = 1;
				}
			else if ((get_char_status(ME,0) <= 0) && (get_ran(1,0,100) < 20)) {
				run_char_animation(2,1,35);	
				print_named_str(ME,"goes into a furious rage.");
				pc_heard_sound_delay(101,250);						
		 	 	place_particle_num(ME,1,0,8);
				set_char_status(ME,0,6);
				set_char_status(ME,16,6);
				last_abil = get_current_tick();
				used_abil = 1;
				}
		}
		
	if ((used_abil == 0) && (get_memory_cell(4) > 0) && (is_combat())) {
		if ((get_ran(1,0,100) < 50) && (tick_difference(last_abil,get_current_tick()) > 0)) {
			print_named_str(ME,"fires a beam of energy.");
			pc_heard_sound_delay(131,250);						
		  	place_particle_num(ME,1,1,8);
			run_char_animation(2,1,35);	
			damage_char(get_target(),get_ran(get_level(ME) + get_attack_bonus(ME),1,10),1);
			
			if (get_ran(1,0,100) < 50) {// slow
				shoot_projectile(ME,get_target(),34);
				set_char_status(get_target(),1,-8);
			  	place_particle_num(get_target(),2,6,8);
				used_abil = 1;
				}
				else { // curse
					shoot_projectile(ME,get_target(),38);
					set_char_status(get_target(),0,-8);
					set_char_status(get_target(),8,-8);
				  	place_particle_num(get_target(),3,6,8);
					used_abil = 1;
					}

			last_abil = get_current_tick();
			}
		}
	
	if (used_abil)
		end();
		
	do_attack();
break;

beginstate 4; // patrol
	if ( my_current_message() == 50)
		has_been_alerted = 1;

	if (get_foe_target(ME,8,0)) {
		alerted_time = get_current_tick();
		do_attack();
		set_state(3);
		}
	if (who_shot_me() >= 0) {
		alerted_time = get_current_tick();
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}
	if (follow_path(ME,get_memory_cell(0),0) == TRUE) 
		set_state(START_STATE);
break;

beginstate 5; // running to start
	if ( my_current_message() == 50)
		has_been_alerted = 1;

	if (has_fled == 0) {
		has_fled = 1;
		print_named_str(ME,"runs to get help.");
		}
	if (my_dist_from_start() >= 12) {
		return_to_start(ME,2);
		}
		else {
			print_str("You hear a loud alarm cry. It echoes through the area.");
			if (get_memory_cell(2) > 0) {
				alert_char(1000 + get_memory_cell(2));
				give_char_message(1000 + get_memory_cell(2),50);
				set_act_at_dist(1000 + get_memory_cell(2),1);
				}
				else if (what_group_in(ME) > 0) {
					alert_char(1000 + what_group_in(ME));
					give_char_message(1000 + what_group_in(ME),50);
					set_act_at_dist(1000 + what_group_in(ME),1);
					}
			has_alerted_group = 1;
			set_state(START_STATE);
			}
break;

beginstate TALKING_STATE;
	if (get_memory_cell(1) == 0) {
		print_str("You try to start a conversation with the guard, but it pays no");
		print_str("  attention to you. The guard is serious about its duties.");
		}
		else begin_talk_mode(get_memory_cell(1));
break;